const express = require("express");
const { sendMails } = require("./send-email");
const { getVerificationCode } = require("./radomCode");
const app = express();
app.use(express.json()); // 解析 JSON 请求体

// 发送验证码接口
app.post("/send-code", async (req, res) => {
  const { email } = req.body;
  const code = getVerificationCode(); // 6 位随机数字验证码
  try {
    await sendMails(email, `您的验证码是：${code}，有效期 5 分钟`);
    res.json({ success: true, message: "验证码已发送" });
  } catch (error) {
    console.error("发送失败:", error);
    res.status(500).json({ success: false, message: "邮件发送失败" });
  }
});

// 启动服务器
app.listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});
